package com.example.carrierapp.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;

import com.example.carrierapp.util.LogUtil;

public class MainBottomNavBar extends ConstraintLayout implements View.OnClickListener {
    private OnTabItemClickListener mOnTabItemClickListener;

    private View mCurrentView;

    private int mFirstSelectedPosition = 0;
    private int mFirstSelectedId = View.NO_ID;
    private boolean mTriggerAfterSet = false;

    public MainBottomNavBar(Context context) {
        this(context,null, 0);
    }

    public MainBottomNavBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MainBottomNavBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).setOnClickListener(this);
        }
    }

    public void setOnTabItemClickListener(OnTabItemClickListener onTabItemClickListener) {
        mOnTabItemClickListener = onTabItemClickListener;
        if (mOnTabItemClickListener != null && mTriggerAfterSet) {
            mTriggerAfterSet = false;
            View v;
            if (mFirstSelectedId > 0) {
                v = findViewById(mFirstSelectedId);
            } else {
                v = getChildAt(mFirstSelectedPosition);
            }
            if (v != null) {
                v.performClick();
            } else {
                LogUtil.w("View with id#%d is null"+mFirstSelectedId);
            }
        }
    }

    public void setCheckedId(int id) {
        View child = findViewById(id);
        if (child != null) {
            child.performClick();
        }
    }

    @Override
    public void onClick(View v) {
        setChecked(v);
        if (mOnTabItemClickListener != null) {
            mOnTabItemClickListener.onTabClick(v.getId());
        }
    }

    private void setChecked(View v) {
        if (v == mCurrentView) {
            return;
        }
        if (mCurrentView != null && mCurrentView instanceof Checkable) {
            ((Checkable) mCurrentView).setChecked(false);
        }
        if (v instanceof Checkable) {
            ((Checkable) v).setChecked(true);
        }
        mCurrentView = v;

    }

    public void setFirstSelectedPosition(int firstSelectedPosition) {
        mFirstSelectedPosition = firstSelectedPosition;
        mTriggerAfterSet = mOnTabItemClickListener == null;
    }

    /**
     * 设置默认选中的item id
     * @param id
     */
    public void setFirstCheckedId(int id) {
        mFirstSelectedId = id;
        mTriggerAfterSet = mOnTabItemClickListener == null;
    }

    public interface OnTabItemClickListener {
        void onTabClick(int id);
    }
}
